﻿using Abp.Reflection.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ARchGL.Platform.Electrocar.Application.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public static class SystemHelper
    {
        /// <summary>
        /// 获取 wwwroot 所在目录
        /// </summary>
        /// <returns></returns>
        public static string GetContentRootFolder()
        {
            // 当前目录
            var dirInfo = new DirectoryInfo(Assembly.GetExecutingAssembly().GetDirectoryPathOrNull());

            // 向上查找包含wwwroot的目录
            while (!dirInfo.GetDirectories("wwwroot", SearchOption.AllDirectories).Any())
            {
                // 已经到根目录
                if (dirInfo.Parent == null) return null;
                dirInfo = dirInfo.Parent;
            }

            // wwwroot是当前目录的子目录
            if (dirInfo.GetDirectories("wwwroot").Any())
            {
                return dirInfo.FullName;
            }

            // 向下查找
            while (dirInfo != null)
            {
                foreach (var dir in dirInfo.GetDirectories())
                {
                    // 当前目录包含wwwroot子目录
                    if (dir.GetDirectories("wwwroot", SearchOption.AllDirectories).Any())
                    {
                        // wwwroot是当前目录的子目录
                        if (dir.GetDirectories("wwwroot").Any())
                        {
                            return dir.FullName;
                        }
                        dirInfo = dir;

                        break;// 继续查找其它子目录
                    }
                }
            }
            return null;// 未找到包含wwwroot的目录
        }
    }
}
